﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Blog.Core;
using System.Text;

public partial class CommentsTree : System.Web.Mvc.ViewPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public bool CanShowDelete(MessageTreeItem message)
    {
        if (Context == null || Context.User == null)
            return false;

        return (Context.User.IsInRole("Administrator") || string.Compare(message.User, Context.User.Identity.Name, true) == 0) && 
            !message.HasChldren;
    }

    public string GetAnswerCellBlock(MessageTreeItem message)
    {
        TagBuilder tb = new TagBuilder("td");
        tb.Attributes.Add("colspan", "1");
        tb.AddCssClass("treemess_buttons");
        tb.GenerateId(string.Format("AnswerCellBlock_{0}_{1}", message.ID, message.TreeID));

        if (Request.IsAuthenticated)
        {
            StringBuilder sb = new StringBuilder();

            TagBuilder answerTb = new TagBuilder("a");
            answerTb.AddCssClass("more");
            answerTb.Attributes.Add("href", string.Format("javascript:Answer({0}, {1})", message.ID, message.TreeID));
            answerTb.GenerateId(string.Format("AnswerButton_{0}_{1}", message.ID, message.TreeID));
            answerTb.InnerHtml = "Ответить";

            sb.AppendLine(answerTb.ToString());

            if (CanShowDelete(message))
            {
                TagBuilder deleteTb = new TagBuilder("a");
                deleteTb.AddCssClass("more");
                deleteTb.Attributes.Add("href", string.Format("javascript:DeleteAnswer({0}, {1})", message.ID, message.TreeID));
                deleteTb.GenerateId(string.Format("DeleteButton_{0}_{1}", message.ID, message.TreeID));
                deleteTb.InnerHtml = "Удалить";

                sb.AppendLine(deleteTb.ToString());
            }

            tb.InnerHtml = sb.ToString();
        }

        return tb.ToString();
    }
}
